﻿<%@ WebHandler Language="C#" Class="App_Airline" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
public class App_Airline : IHttpHandler, IRequiresSessionState
{

    string msg = string.Empty;
    readonly Users Users = new Users();
    public void ProcessRequest(HttpContext context)
    {
        CurrentContext = context;
        try
        {
            string action = context.Request.QueryString["action"].ToString();
           
            string udid = string.Empty;
          
            //string realname = string.Empty;        
            string date = string.Empty;
            string flight_no = string.Empty;
            string arriving = string.Empty;
            string departing = string.Empty;
            udid = GetQuery("udid");
            
            //PC端使用参数
           
            
            switch (action.ToLower())
            {
                    
                case "search":
                     flight_no = GetQuery("flight_no");
                     arriving = GetQuery("arriving");
                     departing = GetQuery("departing");
                     date = GetQuery("date");
                     Search(flight_no, arriving, departing, date, udid);
                    break;
                    
                case "netsearch":
                    string str_city_from = GetQuery("cityfrom", "");
                    string str_city_to = GetQuery("cityto", "");
                    SearchOnNet(str_city_from,str_city_to);
                    break;
            }
        }
        catch (Exception err)
        {
            msg = "{\"result\":\"error\",\"msg\":\"" + err.Message + "\"}";
        }
        finally
        {
            context.Response.Write(msg);
        }
    }
    public HttpContext CurrentContext;
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public int GetQuery(string name, int defaultvalue)
    {
        int res = 0;
        if (CurrentContext.Request.QueryString[name] != null)
            int.TryParse(CurrentContext.Request.QueryString[name], out res);
        else
            res = defaultvalue;
        return res;
    }

    public string GetQuery(string name, string defaultvalue)
    {
        string res = string.Empty;
        if (CurrentContext.Request.QueryString[name] != null)
            res = CurrentContext.Request.QueryString[name].ToString();
        else
            res = defaultvalue;
        return res;
    }

    public string GetQuery(string name)
    {
        string res = string.Empty;
        if (CurrentContext.Request.QueryString[name] != null)
            res = CurrentContext.Request.QueryString[name].ToString();
        return res;
    }

    public void Search(string flight_no, string arriving, string departing,string date, string udid)
    {
        if (string.IsNullOrEmpty(flight_no) && string.IsNullOrEmpty(arriving) && string.IsNullOrEmpty(departing) && string.IsNullOrEmpty(date))
        {
            msg = "{\"result\":\"error\",\"msg\":\"参数不能为空\"}";
        }
        else
        {
            System.Text.StringBuilder sqlBuilder = new System.Text.StringBuilder();
            sqlBuilder.AppendFormat(" select * from AirLine_Info where Flight_No='{0}' and City_from='{1}' and City_to='{2}' ", flight_no,departing,arriving);
            sqlBuilder.AppendFormat(" and convert(varchar(50),EFFECTIVE_TO,120) < {0} and convert(varchar(50),EFFECTIVE_FROM,120) > {0} ", date);
            
            System.Data.DataTable dtAirInfo= Gomye.DataUtility.SqlRunner.DoSqlTable(sqlBuilder.ToString());
            int dtNum=dtAirInfo.Rows.Count;
            if (dtNum > 0)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("{\"result\":\"success\",\"count\":" + "1" + ",\"data\":[");
                
                foreach (System.Data.DataRow row in dtAirInfo.Rows)
                {
                    string strPa_time = dateFormart(row["DEPART_TIME"].ToString());
                    string strPd_time = dateFormart(row["ARRIVAL_TIME"].ToString());
                    string strFlightNoDesc = string.Format("{0}-{1}", row["CITY_FROM_CH"].ToString(), row["CITY_TO_CH"].ToString());
                    sb.Append("{");
                    sb.Append(" \"date\": \""+date+"\",");
                    sb.Append(" \"scheduled_time\": \"" + strPa_time + "\",");
                    sb.Append(" \"estimated_time\": \"" + strPa_time + "\",");
                    sb.Append(" \"pa_time\": \"" + strPa_time + "\",");
                    sb.Append(" \"pd_time\": \"" + strPd_time + "\",");
                    sb.Append("  \"airline\": \"" + row["FLIGHT_NO"].ToString() + "\",");
                    sb.Append(" \"flight_no\": \"" + row["FLIGHT_NO"].ToString() + "\",");
                    sb.Append(" \"flight_no_desc\": \""+strFlightNoDesc+"\",");
                    sb.Append(" \"logo\": \"\",");
                    sb.Append("  \"airport\": \"" + row["AIRPORT_FROM"].ToString() + "\",");
                    sb.Append(" \"origin\": \"" + "" + "\",");
                    sb.Append("  \"via\": \"\",");
                    sb.Append("  \"terminal\": \"" + "" + "\",");
                    sb.Append(" \"belt\": \"" + "" + "\",");
                    sb.Append("  \"status\": \"" + "" + "\",");
                    sb.Append(" \"airline_name\": \"" + row["AIRLINE_NAME"].ToString() + "\",");
                    sb.Append("  \"airline_alias\": \"\",");
                    sb.Append(" \"unixtime\": \"" + "" + "\",");
                    sb.Append(" \"master_flight_no\": \"" + row["FLIGHT_NO"].ToString() + "\",");
                    sb.Append(" \"slave_flight_no\": [");
                    sb.Append("]");
                    sb.Append("}");
                    //sb.Append(" \"date\": \"2013-04-11\",");
                    //sb.Append(" \"scheduled_time\": \"23:30\",");* jihua chufa 计划时间
                    //sb.Append(" \"estimated_time\": \"23:10*\",");* zhengchang chufa 正常时间
                    //sb.Append(" \"pa_time\": \"23:30\",");chufa*  出发时间
                    //sb.Append(" \"pd_time\": \"23:10*\",");daoda* 到达时间
                    //sb.Append("  \"airline\": \"TR\",");*
                    //sb.Append(" \"flight_no\": \"TR2109\",");*
                    //sb.Append(" \"flight_no_desc\": \"北京-樟宜\",");*
                    //sb.Append(" \"logo\": \"\",");*
                    //sb.Append("  \"airport\": \"BKK\",");*
                    //sb.Append(" \"origin\": \"Bangkok (Suvarnabhumi)\",");
                    //sb.Append("  \"via\": \"\",");
                    //sb.Append("  \"terminal\": \"2\",");
                    //sb.Append(" \"belt\": \"34\",");
                    //sb.Append("  \"status\": \"Landed\",");
                    //sb.Append(" \"airline_name\": \"TIGER AIRWAYS\",");*
                    //sb.Append("  \"airline_alias\": \"\",");
                    //sb.Append(" \"unixtime\": \"1365694200\",");
                    //sb.Append(" \"master_flight_no\": \"TR2109\",");
                    //sb.Append(" \"slave_flight_no\": [");
                    //sb.Append("]");
                }
                if (sb.Length > 0)
                    sb = sb.Remove(sb.Length - 1, 1);
                sb.Append("]}");
                msg = sb.ToString();
            }
            else
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("{\"result\":\"success\",\"count\":" + "0" + "}");

                msg = sb.ToString();
            }
        }
            
    }

    public void SearchOnNet(string city_from, string city_to)
    {
        if (string.IsNullOrEmpty(city_to))
        {
            city_to = "新加坡";
        }
        if (string.IsNullOrEmpty(city_from))
        {
            city_from = "新加坡";
        }

        string strWhere = GetQuery("where", "");
        int pageSize = GetQuery("rows", 5);
        int pageIndex = GetQuery("page", 1);
        int total = 0;
        string sql = string.Format(" select aid from AirLine_Info where  City_from_ch='{0}' and City_to_ch='{1}' ", city_from.Trim('+').Trim(), city_to.Trim('+').Trim());

        System.Text.StringBuilder sqlBuilder = new System.Text.StringBuilder();
        sqlBuilder.AppendFormat(" select top {2} * from AirLine_Info where  City_from_ch='{0}' and City_to_ch='{1}' ", city_from, city_to, pageSize);
        sqlBuilder.AppendFormat(" and aid not in (select top {2} aid from AirLine_Info where City_from_ch='{0}' and City_to_ch='{1}' ) ", city_from, city_to, pageSize * (pageIndex - 1));

        System.Data.DataTable dtAirInfo = Gomye.DataUtility.SqlRunner.DoSqlTable(sqlBuilder.ToString());
        System.Data.DataTable dtCount = Gomye.DataUtility.SqlRunner.DoSqlTable(sql);

        int dtNum = dtCount.Rows.Count;
        if (dtNum > 0)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("{\"result\":\"success\",\"count\":" + dtNum.ToString() + ",\"data\":[");

            foreach (System.Data.DataRow row in dtAirInfo.Rows)
            {
                string strPa_time = dateFormart(row["DEPART_TIME"].ToString());
                string strPd_time = dateFormart(row["ARRIVAL_TIME"].ToString());

                string strEf_time =Convert.ToDateTime(row["EFFECTIVE_FROM"]).ToShortDateString();
                string strEt_time = Convert.ToDateTime(row["EFFECTIVE_TO"]).ToShortDateString();
                string InvaildDate = string.Format("{0}至{1}", strEf_time, strEt_time);
                
                string strFlightNoDesc = string.Format("{0}-{1}", row["CITY_FROM_CH"].ToString(), row["CITY_TO_CH"].ToString());
                sb.Append("{");
                sb.Append(" \"date\": \"" + "" + "\",");
                sb.Append(" \"scheduled_time\": \"" + strPa_time + "\",");
                sb.Append(" \"estimated_time\": \"" + strPa_time + "\",");
                sb.Append(" \"pa_time\": \"" + strPa_time + "\",");
                sb.Append(" \"pd_time\": \"" + strPd_time + "\",");
                sb.Append("  \"airline\": \"" + row["FLIGHT_NO"].ToString() + "\",");
                sb.Append(" \"flight_no\": \"" + row["FLIGHT_NO"].ToString() + "\",");
                sb.Append("  \"city_from\": \"" + row["CITY_FROM_CH"].ToString() + "\",");
                sb.Append(" \"city_to\": \"" + row["CITY_TO_CH"].ToString() + "\",");
                sb.Append(" \"invailddate\": \"" + InvaildDate + "\",");
                sb.Append(" \"flight_no_desc\": \"" + strFlightNoDesc + "\",");
                sb.Append(" \"logo\": \"\",");
                sb.Append("  \"airport\": \"" + row["AIRPORT_FROM"].ToString() + "\",");
                sb.Append(" \"origin\": \"" + "" + "\",");
                sb.Append("  \"via\": \"\",");
                sb.Append("  \"terminal\": \"" + "" + "\",");
                sb.Append(" \"belt\": \"" + "" + "\",");
                sb.Append("  \"status\": \"" + "" + "\",");
                sb.Append(" \"airline_name\": \"" + row["AIRLINE_NAME"].ToString() + "\",");
                sb.Append("  \"airline_alias\": \"\",");
                sb.Append(" \"unixtime\": \"" + "" + "\",");
                sb.Append(" \"master_flight_no\": \"" + row["FLIGHT_NO"].ToString() + "\",");
                sb.Append(" \"slave_flight_no\": [");
                sb.Append("]");
                sb.Append("},");
            }

            if (sb.Length > 0)
                sb = sb.Remove(sb.Length - 1, 1);
            sb.Append("]}");
            msg = sb.ToString();
        }
        else
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("{\"result\":\"success\",\"count\":" + "0" + "}");
            msg = sb.ToString();
        }


    }

    public string dateFormart(string dateStr)
    {
        string strHours = "";
        string strMin = "";
        string strDate = "";
        if (dateStr != "")
        {
            int Dlength = dateStr.Length;
            if (Dlength != 4)
            {
                if (Dlength < 4)
                {
                    while (Dlength != 4)
                    {
                        dateStr = "0" + dateStr;
                        Dlength = dateStr.Length;
                    }
                }
                else
                {
                    return "";
                }

            }

            strHours = dateStr.Substring(0, 2);
            strMin = dateStr.Substring(2, 2);

            strDate = string.Format("{0}:{1}", strHours, strMin);
        }

        return strDate;
    }

}
